<?php

namespace App\Http\Controllers;

use App\Http\Requests\SearchRequest;
use App\Models\Chapter;
use App\Models\Repository;
use App\Models\User;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function repositories(SearchRequest $request, Repository $repository)
    {

        return view('resources.repository.index')->with(
            'repositories',
            $repository->where('title', 'like', '%' . $request->keyword . '%')->with('user')->withCount(['currentChapters', 'forks', 'issues'])->latest()->paginate()->appends(['keyword'])
        );
    }

    public function chapters(SearchRequest $request, Chapter $chapter)
    {
        return view('resources.chapter.search')->with(
            'chapters',
            $chapter->where('title', 'like', '%' . $request->keyword . '%')->orWhere('content', 'like', '%' . $request->keyword . '%')->indexQuery()->paginate()->appends(['keyword'])
        );
    }

    public function users(SearchRequest $request, User $user)
    {
        return view('resources.user.search')->with('users', $user->where('name', 'like', '%' . $request->keyword . '%')->indexQuery()->paginate()->appends(['keyword']));
    }
}
